/*
 *  VortPolygon.h
 *  VortPan
 *
 *  Created by Gregor Veble on 6/16/10.
 *  Copyright 2010  . All rights reserved.
 *
 */

#ifndef VORTPOLYGON_H
#define VORTPOLYGON_H

#include <vector>

#include "VortBase.h"
#include "VortPointReference.h"

namespace VortPan {
	
	typedef std::vector<VortPointReference> PointReferenceVector;
	
	
	class VortPolygon : public VortBase {
	public:
		VortPolygon(bool IsWake=false);
		~VortPolygon();
		
		void AddPoint(VortPointReference I);
		int AddPoint(VortPointReference I, int BeforePoint);
		void SetCenterPoint(VortPoint Center);
		Vector3D& GetCenterPosition();
		Vector3D& GetNormal();
		re& GetArea();
		int RecalculateAverageCenterPoint(re Eps=-1e-5);
		
		int GetNPoints();
		VortPoint& GetPoint(int index);
		VortPointReference& GetPointReference(int index);					  
		
		re GetSolidAngle(const Vector3D& R);
		re GetPositiveSelfSolidAngle();
		
		int GetIndex(const VortPointReference& Ref);
		
		Vector3D GetRegularizedVelocityInfluence(const Vector3D& R, const re& InvCoreSize2);


	protected:
		Vector3D VortexVelocity(const Vector3D& R1, const Vector3D& R2, const Vector3D& R, const re& InvCoreSize2);
		
		re SolidAngle(const Vector3D& R, const Vector3D& V1, const Vector3D& V2, const Vector3D& V3);
		void RecalculateNormal();
		
		void EliminateDegenerateEdges();

		PointReferenceVector PointList;
		VortPoint CenterPoint;
		Vector3D Normal;
		re Area;
		
		bool WakePoly;
		
		int SolidIndex;
		
		VortPolygon* pSibling;
		VortPolygon* GetTopmostSibling();
				
		friend class VortMesh;
		friend class VortSystem;

	

	};	
	
}

#endif
